package com.jlastu.znb.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.znb.R;
import com.jlastu.znb.adapter.NewsListAdapter;
import com.jlastu.znb.base.BaseFragment;
import com.jlastu.znb.contract.SellerHomeContract;
import com.jlastu.znb.entity.News;
import com.jlastu.znb.presenter.SellerHomePresenter;

import java.util.ArrayList;
import java.util.List;

public class SellerHomeFragment extends BaseFragment implements SellerHomeContract.ISellerHomeView ,View.OnClickListener {
    private RecyclerView mNewsRecyclerView;
    private NewsListAdapter mRecyclerAdapter;
    private SellerHomeContract.ISellerHomePresenter mSellerHomePresenter;
    private List<News> mNewsList;
    private TextView mTextView;
    private ImageView mImageView;
    private LinearLayout gobuyerLayout;
    private LinearLayout mLinearLayout1;
    private LinearLayout mLinearLayout2;
    private LinearLayout mLinearLayout3;
    private LinearLayout mLinearLayout4;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_seller_home;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSellerHomePresenter.getNewsList(); // 假设这是获取所有新闻的方法名
    }

    @Override
    protected void initView() {
        mSellerHomePresenter = new SellerHomePresenter(this, getContext());
        mNewsList = new ArrayList<>();
        mNewsRecyclerView = mView.findViewById(R.id.rv_news);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mNewsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new NewsListAdapter(mNewsList);
        mNewsRecyclerView.setAdapter(mRecyclerAdapter);

        mTextView = mView.findViewById(R.id.moretextview);
        mImageView = mView.findViewById(R.id.moreimageview);
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MainActivity.class);
                intent.putExtra(MainActivity.EXTRA_SHOW_SCHOOL_FRAGMENT, true);
                startActivity(intent);
            }
        });
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MainActivity.class);
                intent.putExtra(MainActivity.EXTRA_SHOW_SCHOOL_FRAGMENT, true);
                startActivity(intent);
            }
        });

        gobuyerLayout = mView.findViewById(R.id.go_buyer_layout); // 确保 ID 与布局文件中的定义一致
        gobuyerLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MainActivity.class);
                startActivity(intent);
            }
        });
        mLinearLayout1 = mView.findViewById(R.id.linearlayout1); // 确保 ID 与布局文件中的定义一致
        mLinearLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MyGoodsActivity.class);
                startActivity(intent);
            }
        });
        mLinearLayout2 = mView.findViewById(R.id.linearlayout2); // 确保 ID 与布局文件中的定义一致
        mLinearLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MyProcureActivity.class);
                startActivity(intent);
            }
        });
        mLinearLayout3 = mView.findViewById(R.id.linearlayout3); // 确保 ID 与布局文件中的定义一致
        mLinearLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrdersActivity.class);
                intent.putExtra("SELECT_DEFAULT_TAB", "orders_big_title2"); // 添加标志
                startActivity(intent);
            }
        });
        mLinearLayout4 = mView.findViewById(R.id.linearlayout4); // 确保 ID 与布局文件中的定义一致
        mLinearLayout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MyShopActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void showNewsList(List<News> newsList) {
        mNewsList =newsList;
        Log.d(TAG, "showNewsByCategory: "+newsList);
        mRecyclerAdapter.updateData(newsList); // 刷新RecyclerView数据
    }
}